const router = require('koa-router')()
const db = require("../config/db");

router.get('/', async (ctx, next) => {
  console.log(db)
  var sqlSelect = "select * from chapters";
  var result = await db.query(sqlSelect, []);
  var chapterList = result;

  await ctx.render('index', {
    chapterList: chapterList
  })
})

router.get('/detail/:id', async (ctx,next)=>{
  var chapterId = ctx.params.id;
  var selectSql = "select * from chapters where chapterId=?"
  var result = await db.query(selectSql,[chapterId])
  console.log(result) //返回的是chapterId为点击的那个id的数据
  await ctx.render('detail',{
    chapter:result[0]
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
